package com.sheng.whyclass.person.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.toolbox.StringRequest;
import com.sheng.util.Constant;
import com.sheng.util.CustomToast;
import com.sheng.util.HttpProcessManager;
import com.sheng.util.VolleyManager;
import com.sheng.whyclass.R;
import com.sheng.whyclass.person.event.FindAllStudentInfoEvent;
import com.sheng.whyclass.work.adapter.AllStudentAdapter;

import de.greenrobot.event.EventBus;

public class ManagerStudentFragment extends Fragment implements View.OnClickListener {
    private View mRootView = null;
    private ListView workList;
    private AllStudentAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_all_student, container, false);
        adapter = new AllStudentAdapter(getActivity());
        workList = (ListView) mRootView.findViewById(R.id.work_list);
        workList.setAdapter(adapter);

        initView();


        return mRootView;
    }

    private void initView() {
        findAllStudent();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


    public void onEventMainThread(FindAllStudentInfoEvent event) {


        int code = event.code;
        if (event.list == null) {
            return;
        }

        if (code == Constant.CODE_FAIL) {
            CustomToast.showToast(getString(R.string.net_fail));

            return;
        } else {
            adapter.setData(event.list);
        }

    }



    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){

        }
    }



    private void findAllStudent() {
        StringRequest request = HttpProcessManager.getInstance().findAllStudentInfo(Constant.HOST_STUDENT_ALL_FIND);
        VolleyManager.getInstance(getActivity().getApplicationContext()).addToRequestQueue(request);

    }
}
